import { socialUserMock } from './data/user'
import { isDateTimeInRange } from '../../utils'
let socialUserList = socialUserMock(400)
export default [
  {
    url: RegExp('/system/social-user/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/system/social-user/detail' + '.*'), // 删除
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getInfoById(params)
      }
    }
  }
]

const getListByPage = params => {
  let result = socialUserList
  result = result.filter(item => !item.deleted)
  if (params.query.type) {
    result = result.filter(item => item.type === params.query.type)
  }
  if (params.query.openid) {
    result = result.filter(item => item.openid === params.query.openid)
  }
  if (params.query.nickname) {
    result = result.filter(item => item.nickname.indexOf(params.query.nickname) !== -1)
  }
  if (params.query.create_time && params.query.create_time.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.create_time[0], params.query.create_time[1], item.create_time))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getInfoById = params => {
  const result = socialUserList.filter(item => {
    return item.id === parseInt(params.query.id)
  })
  if (result.length > 0) {
    return result[0]
  } else {
    return null
  }
}
